# frozen_string_literal: true
class EditorsController < ApplicationController
  before_action :set_editor, only: [:edit, :update, :destroy]

  def index
    if @current_editor.system?
      @editors = Editor.all.order(created_at: :desc).page(page).per(per)
    elsif @current_editor.estate?
      @editors = @current_editor.village.editors.page(page).per(per)
    else
      redirect_to interviews_url
    end
  end

  def new
    @editor = Editor.new

    if @current_editor.system?
      @villages = Village.all
    elsif @current_editor.estate?
      @buildings = @current_editor.village.buildings
    else
      redirect_to interviews_url
    end
  end

  def edit
    @villages = Village.all
  end

  def create
    @editor = Editor.new(editor_params)

    if @editor.save
      if @current_editor.system?
        @editor.add_role(:estate)
      elsif @current_editor.estate?
        @editor.update(village: @current_editor.village)
        @editor.add_role(:floor)
      else
        redirect_to interviews_url
        return
      end
      redirect_to editors_url, notice: 'Editor was successfully created.'
    else
      render :new
    end
  end

  def update
    if @editor.update(editor_params)
      redirect_to editors_url, notice: 'Editor was successfully updated.'
    else
      render :edit
    end
  end

  def destroy
    @editor.buildings.each do |building|
      building.editor_id = nil
      building.save
    end
    @editor.destroy
    redirect_to editors_url, notice: 'Editor was successfully destroyed.'
  end

  def import
    uploaded_io = params[:file]
    #type =   uploaded_io.content_type
    #unless type ==  "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    #  redirect_to request.referer, notice: '文件类型必须execl'
    #else
      convert_to_nice_date = Time.now.strftime("%Y/%m/%d")
      store_dir = "system/uploads/#{convert_to_nice_date}"
      FileUtils.mkdir_p("#{Rails.root}/public/#{store_dir}")
      file_name= uploaded_io.original_filename
      File.open(Rails.root.join('public',"#{store_dir}", uploaded_io.original_filename), 'wb') do |file|
        file.write(uploaded_io.read)
      end
      path = "#{Rails.root}/public/#{store_dir}/#{file_name}"
      s = SimpleSpreadsheet::Workbook.read(path)
      s.first_row.upto(s.last_row)  do |line|
        if line > 1
          village = Village.find_or_create_by(name: s.cell(line, 1, 1),address: s.cell(line, 14, 1))
          editor = Editor.find_or_create_by(
            name: s.cell(line, 8, 1),
            telephone: s.cell(line, 9, 1).strip,
            village: village
          )
          editor.add_role(:floor)
          building = Building.find_or_create_by(village: village, editor: editor,code: s.cell(line, 2, 1))
          User.find_or_create_by(
            name: s.cell(line, 4, 1),
            village: village,
            building: building,
            id_card: s.cell(line, 5, 1),
            telephone: s.cell(line, 6, 1).strip,
            floor_code: s.cell(line, 3, 1),
            car_code: s.cell(line, 12, 1)
          )
          editor_estate = Editor.find_or_create_by(
            name: s.cell(line, 10, 1),
            telephone: s.cell(line, 11, 1),
            village: village
          )
          editor_estate.add_role(:estate)
          #Building.create(village: village,editor: editor_estate,code: s.cell(line, 2, 1))
        end
      end
      redirect_to request.referer, notice: '导入成功'
    #end
  end

  private

  def set_editor
    @editor = Editor.find(params[:id])
  end

  def editor_params
    params.require(:editor).permit(:telephone, :name, :password, :village_id, :building_id)
  end
end
